// pages/myPhotos/myPhotos.js
const API = require('../../common/constant');
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    photos:[],
    deleteStatus: false,
  },

  showImage: function(){
    this.setData({
      deleteStatus: false
    })
  },

  /**
   * 上传图片
   */
  uploadImage: function(e){
    let that = this;
    console.log('上传图片');
    wx.chooseImage({
      count: 9, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'], 
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        wx.showToast({  
          title: '正在上传...',  
          icon: 'loading',  
          mask: true,  
          duration: 10000  
        });
        console.log(tempFilePaths);
        let loadData = [];
        let UploadImgCount = 0;
        for(let i = 0 ; i < tempFilePaths.length; i++){
          wx.uploadFile({
            url: API.URL.SYS.UPLOAD_FILE,
            filePath: tempFilePaths[i],
            header: {
              "Content-Type": "json"
            },
            name: 'file',
            formData: {
              sessionId: wx.getStorageSync('sessionId')
            },
            success: function(res){
              console.log(res);
              console.log(JSON.parse(res.data));
              loadData.push((JSON.parse(res.data)).data.bussData.fileKey);
              UploadImgCount++;
              if(UploadImgCount == tempFilePaths.length){
                util.http({
                  url: API.URL.USER.EDIT_MINIUSERINFO,
                  data: {
                    imageInfoList: loadData
                  },
                  success: res => {
                    console.log(res);
                    wx.hideToast(); 
                    that.getImages();
                    app.globalData.isNewMyInfo = true;
                  },
                  fail: err => {
                    console.log(err);
                  }
                })
              }
            },
            fail: err => {
              console.log(err);
              wx.hideToast();  
              wx.showModal({  
                title: '错误提示',  
                content: '上传图片失败',  
                showCancel: false,  
                success: function (res) { }  
              }) 
            }
          })
        }
      }
    })
  },
  /**
   * 获取相册信息
   */
  getImages: function(){
    util.http({
      url: API.URL.USER.FIND_USERINFO,
      success: res => {
        console.log(res);
        if(res.data.bussData.imageDTOList.length == 0) {
          console.log('00000');
          this.setData({
            photos: []
          });
        } else {
          this.changePhotoList(res.data.bussData.imageDTOList);
        }
        
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  /**
   * 改照片的样式
   */
  changePhotoList: function(imageDTOList){
    let photoData = [], isPush = true;
    console.log(imageDTOList);
    for(let i = 0,len = imageDTOList.length; i<len; i++){
      let month = parseInt(imageDTOList[i].imageTime.split('-')[1]);
      console.log(month)
      console.log(photoData.length, photoData, i)
      if(i===0) {
        photoData.push({
          month: month,
          imageurl: []
        })
        photoData[0].imageurl.push({
          url: imageDTOList[i].fileUrl,
          key: imageDTOList[i].fileKey
        })
      } else {
        isPush = true
        for (let key = 0, kLen = photoData.length; key < kLen; key++) {
          console.log(photoData[key], month);
          if(photoData[key].month == month) {
            isPush = false
            photoData[key].imageurl.push(
              {
                url: imageDTOList[i].fileUrl,
                key: imageDTOList[i].fileKey
              }
            );
          }          
        }
        if(isPush) {
          photoData.push({
            month: month,
            imageurl: []
          })
          photoData[photoData.length-1].imageurl.push(
            {
              url: imageDTOList[i].fileUrl,
              key: imageDTOList[i].fileKey
            }
          );
        }
      }
    }
    this.setData({
      photos: photoData
    });
  },

  /**
   * 删除照片
   */
  deteImg: function(e){
    let arr = [];
    arr.push(e.target.dataset.key);
    console.log(arr);
    util.http({
      url: API.URL.USER.DELETE_PHOTO,
      data: {
        fileKeys: arr
      },
      success: res => {
        console.log(res);
        this.getImages();
        // if(this.data.photos)
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  /**
   * 预览照片
   */
  previewImage: function(e){
    console.log(e);
    let urls = [];
    let photos = this.data.photos;
    for (let i = 0, iLen = photos.length; i < iLen; i++){
      photos[i].imageurl.map(item=>{
        urls.push(item.url)
      })
    }
    console.log(urls);
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: urls // 需要预览的图片http链接列表
    })
  },
  /**
   * 删除照片
   */
  deletImage: function(e){
    console.log(e,'longpress');
    this.setData({
      deleteStatus: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getImages();
  }
})